package com.mycinema.web.interceptor;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.musicstore.entity.UserEntity;

public class SecurityInterceptor extends HandlerInterceptorAdapter{
	@Override
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object obj){
		UserEntity user = (UserEntity) request.getSession().getAttribute("user");
		if(user==null){
			String loginUrl =request.getContextPath()+"/login";
			System.out.println("loginUrl="+loginUrl);
			String returnUrl = request.getServletPath();
			System.out.println("returnUrl="+returnUrl);
			String queryPath = request.getQueryString();
			String url =loginUrl+"?returnUrl="+returnUrl;
			if(queryPath!=null){
				url+="?"+queryPath;
			}
			System.out.println(" URL===="+url);
			try {
				response.sendRedirect(url);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return false;
		}
		return true;
	}
}
